package main

import (
	"gitee.com/kristas/booting-go/framework"
	"gitee.com/kristas/booting-go/framework/common/util/lang"
	"gitee.com/kristas/booting-go/framework/data/datasource"
	"gitee.com/kristas/booting-go/plugin/logrus_plugin"
	"os"
	"sync"
)

func init() {
    wd, err := os.Getwd()
    if err != nil {
        panic(err)
    }
    wd = lang.NewString(wd).RemoveSuffix(".tmp").String()
    lock := NewWdLock(wd)
	framework.Component(
		logrus_plugin.NewLoggerFactory(),
		{{- range .Data.Layers }}
		New{{ . }}Generator(lock),{{ end }}
	)
	framework.AddProcessor(
        {{- range .Data.Layers }}
        New{{ . }}Generator(lock),{{ end }}
	)
}

func init() {
	framework.Component(datasource.NewDataSource("application.datasource"))
}

func main() {
	framework.Application().GenMode().Run()
}

type WdLock struct {
	sync.Mutex
	root string
}

func NewWdLock(root string) *WdLock {
	return &WdLock{root: root}
}

func (l *WdLock) RequiredRoot() string {
	l.Lock()
	return l.root
}

func (l *WdLock) Release() {
	l.Unlock()
}
